package com.hopedove.callsystem.activity.call;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.hopedove.base.BaseActivity;
import com.hopedove.callsystem.R;
import com.hopedove.callsystem.constant.ConstantsKt;
import com.hopedove.callsystem.databinding.ActivityCallMaterialInfoBinding;
import com.hopedove.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;


@SuppressLint("UseSparseArrays")
public class MaterialInfoActivity extends BaseActivity implements OnClickListener {

    private JSONArray jsonArray; // 列表数据源
    private final HashMap<String, Boolean> isCheckMap = new HashMap<>(); // 存储选中的布料信息
    private ActivityCallMaterialInfoBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCallMaterialInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        try {
            jsonArray = new JSONArray(intent.getStringExtra(ScanBarcodeActivity.SOURCE_DATA));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // 获取三联号
        // 三联号
        String ticketNo = intent.getStringExtra(ConstantsKt.getTICKET_NO());
        binding.title.setText("布票号 ：" + ticketNo);
        binding.titleNum.setText("呼叫个数 ：" + isCheckMap.size() + "  ");
        binding.callImg.setOnClickListener(this);
        binding.backImg.setOnClickListener(this);

        binding.infoListview.setAdapter(new MyListAdapter());
    }


    /**
     * 自定义adapter
     */
    class MyListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return jsonArray.length();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            String taskNo = null;
            String materialId = null;
            String materialName = null;
            String materialNumber = null;

            try {
                // 获取每个item的数据
                JSONObject jsonObject = jsonArray.getJSONObject(position);
                taskNo = jsonObject.getString(ConstantsKt.getTASK_NO());
                materialId = jsonObject.getString(ConstantsKt.getMATERIAL_ID());
                materialName = jsonObject.getString(ConstantsKt.getMATERIAL_NAME());
                materialNumber = jsonObject.getString(ConstantsKt.getMATERIAL_NUMBER());
                if (materialNumber.equals("null"))
                    materialNumber = "0";
            } catch (JSONException e) {
                e.printStackTrace();
            }

            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
                viewHolder.info_tv = convertView.findViewById(R.id.info_tv);
                viewHolder.num_tv = convertView.findViewById(R.id.num_tv);
                viewHolder.checkBtn = convertView.findViewById(R.id.checkBox);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.info_tv.setText(materialName);
            viewHolder.num_tv.setText(materialNumber);
            viewHolder.checkBtn.setTag(taskNo + materialId);

            if (isCheckMap.containsKey(taskNo + materialId)) {
                viewHolder.checkBtn.setChecked(isCheckMap.get(taskNo + materialId));
            } else {
                viewHolder.checkBtn.setChecked(false);
            }

            convertView.setOnClickListener(view -> {
                ViewHolder viewHolder1 = (ViewHolder) view.getTag();
                CheckBox checkBox = viewHolder1.checkBtn;
                checkBox.setChecked(!checkBox.isChecked());
                if (checkBox.isChecked()) {
                    isCheckMap.put(checkBox.getTag().toString(), checkBox.isChecked());
                    ((TextView) findViewById(R.id.title_num)).setText("呼叫个数 ：" + isCheckMap.size() + "  ");
                } else {
                    isCheckMap.remove(checkBox.getTag().toString());
                    ((TextView) findViewById(R.id.title_num)).setText("呼叫个数 ：" + isCheckMap.size() + "  ");
                }
            });

            return convertView;
        }

        public class ViewHolder {
            private TextView info_tv;
            private TextView num_tv;
            private CheckBox checkBtn;
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.call_img) {
            if (isCheckMap.size() == 0) {
                ToastUtil.shortToast(this, "请选择所需原料");
                return;
            }
            if (isCheckMap.size() > 7) {
                ToastUtil.shortToast(this, "单次最多只能呼叫7个原料");
                return;
            }

            JSONArray dataArr = new JSONArray();
            // 提取选择的信息
            for (int i = 0, count = 0; i < jsonArray.length() && count < isCheckMap.size(); i++) {
                try {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String key = jsonObject.getString(ConstantsKt.getTASK_NO())
                            + jsonObject.getString(ConstantsKt.getMATERIAL_ID());
                    if (isCheckMap.containsKey(key)) {
                        dataArr.put(jsonObject);
                        count++;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            Intent intent = new Intent();
            intent.putExtra(ConstantsKt.getTICKET_NO(), getIntent().getStringExtra(ConstantsKt.getTICKET_NO()));
            intent.putExtra(ScanBarcodeActivity.SOURCE_DATA, dataArr.toString());
            intent.setClass(getApplicationContext(), ConfirmInfoActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.back_img) {
            finish();
        }
    }


}

